package com.yxjx.duoxue.customview;

import java.util.ArrayList;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.YxTearchersHSVHelper;
import com.yxjx.duoxue.YxTearchersHSVHelper.HsvAdapter;
import com.yxjx.duoxue.course.TeacherIntroductionActivity;
import com.yxjx.duoxue.customview.YxDetailedInfoView.YxListAdapter;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * LinearLayout版本的图文混排控件
 * 
 * @author mengmengchen
 * 
 */
public class YxDetailedInfoView1 extends LinearLayout {

	public static final int TYPE_IMG = 0;
	public static final int TYPE_TEXT = 1;
	public static final int TYPE_TEACHER_ITEM = 2;
	public static final int TYPE_HORIZONTAL_SCROLL_TEACHERS = 3;
	public static final int TYPE_COUNT = 4;
	
	private YxListAdapter1 mAdapter;

	public YxDetailedInfoView1(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public YxListAdapter1 getAdapter() {
		return mAdapter;
	}

	public void setAdapter(YxListAdapter1 adapter) {
		mAdapter = adapter;
		removeAllViews();
		for (int i = 0; i < adapter.getCount(); i++) {
			View itemView = adapter.getView(i, null, null);
			addView(itemView);
			itemView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					YxListAdapter1 adpater = getAdapter();
					Context context = arg0.getContext();
					int index = getChildIndexInParent(arg0);
					if (adpater.getItemViewType(index) == TYPE_TEACHER_ITEM
							&& index != -1) {
						DMTeacher teacher = (DMTeacher) adpater.getDatas().get(index);
						Intent intent = new Intent(context, TeacherIntroductionActivity.class);
						intent.putExtra(NwUtils.KEY_ID, "" + teacher.getId());
						context.startActivity(intent);
					}
				}
			});
		}
		if (adapter.getCount() == 0) {
			// 无记录则显示“无”
			addView(LayoutInflater.from(getContext()).inflate(R.layout.item_list_text, null));
			// horizonal的老师列表为空
		} else if (adapter.getCount() == 1 && adapter.getItemViewType(0) == TYPE_HORIZONTAL_SCROLL_TEACHERS) {
			Object params = adapter.getDatas().get(0);
			if (params instanceof ArrayList<?>) {
				ArrayList<DMTeacher> teachers = (ArrayList<DMTeacher>) params;
				if (teachers == null || teachers.size() == 0) {
					// 无记录则显示“无”
					addView(LayoutInflater.from(getContext()).inflate(R.layout.item_list_text_no_padding, null));
				}
			}
		}
		adapter.setHost(this);
	}
	
	private int getChildIndexInParent(View child) {
		ViewGroup parent = (ViewGroup) child.getParent();
		for (int i = 0; i < getChildCount(); i++) {
			if (parent.getChildAt(i) == child) {
				return i;
			}
		}
		return -1;
	}

	public class YxListAdapter1 extends BaseAdapter {

		private YxDetailedInfoView1 mHost;

		private ArrayList<Integer> mItemTypes;
		private ArrayList<Object> mData;

		public ArrayList<Object> getDatas() {
			return mData;
		}

		public void setHost(YxDetailedInfoView1 host) {
			mHost = host;
		}

		public void setData(ArrayList<Integer> itemTypes, ArrayList<Object> data) {
			mItemTypes = itemTypes;
			mData = data;
			// for Click
			// refresh
			if (mHost != null) {
				mHost.setAdapter(this);
			}
		}

		@Override
		public int getViewTypeCount() {
			return TYPE_COUNT;
		}

		@Override
		public int getItemViewType(int position) {
			return mItemTypes.get(position);
		}

		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (getItemViewType(arg0) == TYPE_TEXT) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(
							R.layout.item_list_text, null);
				} else {
					ret = arg1;
				}
				// customize description
				if (mData.get(arg0) instanceof String) {
					UIHelper.setText(ret, R.id.desc, (String) mData.get(arg0));
				}

			} else if (getItemViewType(arg0) == TYPE_IMG) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(
							R.layout.item_list_image, null);
				} else {
					ret = arg1;
				}
				// customize images
				if (mData.get(arg0) instanceof String) {
//					UIHelper.asyncLoadRoundImage(
//							(ImageView) ret.findViewById(R.id.imageView1),
//							(String) mData.get(arg0),
//							R.drawable.default_image);
					UIHelper.asyncLoadRoundImageSpecSize(
							(ImageView) ret.findViewById(R.id.imageView1),
							(String) mData.get(arg0),
							R.drawable.default_banner);
				}
			} else if (getItemViewType(arg0) == TYPE_TEACHER_ITEM) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(
							R.layout.item_list_teacher, null);
				} else {
					ret = arg1;
				}
				if (mData.get(arg0) instanceof DMTeacher) {
					DMTeacher teacher = (DMTeacher) mData.get(arg0);
					UIHelper.setText(ret, R.id.teacher_name,
							teacher.getTeacherName());
					UIHelper.asyncLoadRoundImageSpecSize(
							(ImageView) ret.findViewById(R.id.teacher_img),
							teacher.getIconUrl(),
							R.drawable.default_icon);
				}
			} else if (getItemViewType(arg0) == TYPE_HORIZONTAL_SCROLL_TEACHERS) {
				ArrayList<DMTeacher> teachersList = null;
				if (mData.get(arg0) instanceof ArrayList<?>) {
					teachersList = (ArrayList<DMTeacher>) mData.get(arg0);
				}
				YxTearchersHSVHelper helper = new YxTearchersHSVHelper(
						getContext());
				ret = helper.getView();
				HsvAdapter helpAdapter = helper.new HsvAdapter(teachersList);
				helper.setAdapter(helpAdapter);
			}
			return ret;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}
	}

}
